package com.ruoyi.project.mobile.goods;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.goods.cart.domain.ShoppingCart;
import com.ruoyi.project.system.goods.cart.service.IShoppingCartService;
import com.ruoyi.project.system.goods.follow.domain.GoodsFollow;
import com.ruoyi.project.system.goods.follow.service.IGoodsFollowService;
import com.ruoyi.project.system.goods.goods.domain.GoodsDto;
import com.ruoyi.project.system.goods.goods.service.IGoodsGoodsService;
import com.ruoyi.project.system.goods.img.enums.GoodsImgType;
import com.ruoyi.project.system.goods.img.service.IGoodsImgService;
import com.ruoyi.project.system.goods.product.service.IGoodsProductService;
import com.ruoyi.project.system.order.comment.dto.OrderCommentDto;
import com.ruoyi.project.system.order.comment.service.IOrderCommentService;
import com.ruoyi.project.system.order.commentimg.domain.CommentImg;
import com.ruoyi.project.system.order.commentimg.service.ICommentImgService;
import com.ruoyi.project.system.spec.service.IGoodsSpecService;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/22 11:39
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/goods")
public class ApiGoodsController extends BaseController {

	@Autowired
	private IGoodsGoodsService goodsGoodsService;
	@Autowired
	private IOrderCommentService commentService;
	@Autowired
	private IGoodsImgService goodsImgService;
	@Autowired
	private IGoodsProductService goodsProductService;
	@Autowired
	private ICommentImgService commentImgService;
	@Autowired
	private IGoodsFollowService goodsFollowService;
	@Autowired
	private IShoppingCartService shoppingCartService;

	/**
	 * 查询商品详情
	 * @param id
	 * @return
	 */
	@GetMapping
	private R findGoodsInfo(@RequestParam String uid,@RequestParam String id){
		GoodsDto goodsDto = goodsGoodsService.findGoodsDtoById(id);
		HashMap<String, Object> map = new HashMap<>();
		//商品信息
		map.put("goods",goodsDto);
//		map.put("goodsComment",this.commentService.findListByGoodsId(id));
		//轮播信息
		map.put("bannerImg",this.goodsImgService.findListByGoodsIdAndType(id,GoodsImgType.BANNER.getValue()));
		//详情信息
		map.put("detailsImg",this.goodsImgService.findListByGoodsIdAndType(id,GoodsImgType.DETAILS.getValue()));
		//是否关注
		map.put("isFollow",this.goodsFollowService.findByUidAndGid(uid,id)==null?false:true);
		return RFactory.generateR(map);
	}


	/**
	 * 查询商品评论
	 * @param uid
	 * @param id
	 * @return
	 */
	@GetMapping("/comment")
	private R findGoodsComment(@RequestParam String uid,@RequestParam String id){
		startPage();
		List<OrderCommentDto> orderCommentDtos = this.commentService.findListByGoodsId(id);
		if(orderCommentDtos!=null){
			for(OrderCommentDto orderCommentDto:orderCommentDtos){
				CommentImg commentImg = new CommentImg();
				commentImg.setCommentId(orderCommentDto.getId());
				orderCommentDto.setCommentImgs(this.commentImgService.selectCommentImgList(commentImg));
			}
		}
		return RFactory.generateR(getDataTable(orderCommentDtos));
	}

	/**
	 * 查询商品规格
	 * @param uid
	 * @param id
	 * @param spec
	 * @return
	 */
	@GetMapping("/spec")
	private R findGoodsSpec(@RequestParam String uid,@RequestParam String id,
							@RequestParam String spec){
		return RFactory.generateR(this.goodsProductService.findBySpec(id,spec));
	}


	/**
	 * 收藏 取消收藏接口
	 * @param goodsFollow
	 * @return
	 */
	@PostMapping("/follow")
	private R goodsFollow(@RequestBody GoodsFollow goodsFollow){
		//查询商品是否存在,是否已经关注 已关注则取消关注 未关注则关注
		GoodsFollow oldFollow = this.goodsFollowService.findByUidAndGid(goodsFollow.getMemberId(),goodsFollow.getGoodsId());
		if(oldFollow==null){
			goodsFollow.setCreated(DateUtils.getNowDate());
			this.goodsFollowService.insertGoodsFollow(goodsFollow);
		}else{
			this.goodsFollowService.deleteGoodsFollowById(oldFollow.getId());
		}
		return R.ok();
	}

	/**
	 * 加入购物车
	 * @param shoppingCart
	 * @return
	 */
	@PostMapping("/cart")
	public R addShoppingCart(@RequestBody ShoppingCart shoppingCart){
		//查询购物车是否存在
		ShoppingCart oldcart = this.shoppingCartService.findByGidAndPid(shoppingCart.getGoodsId(),shoppingCart.getProductId(),shoppingCart.getMemberId());

		if(oldcart==null){
			this.shoppingCartService.insertShoppingCart(shoppingCart);
		}else{
			ShoppingCart update = new ShoppingCart();
			update.setId(oldcart.getId());
			update.setTotal(oldcart.getTotal()+shoppingCart.getTotal());
			this.shoppingCartService.updateShoppingCart(update);
		}

		return R.ok();
	}

}
